<template>
	<view class="app">
		<view class="cell row b-b">
			<text class="tit">会员码</text>
			<input class="input" type="text" v-model="info.agencyId" placeholder="请输入会员码" placeholder-class="placeholder" />
		</view>
		<view class="cell row b-b">
			<text class="tit">手机号</text>
			<input class="input" type="number" maxlength="11" v-model="info.phoneNum" placeholder="请输入手机号码" placeholder-class="placeholder" />
			<view class="getCode" @tap="sendVerifycode()">
				{{verifyWord}}
			</view>
		</view>
		<view class="cell row b-b">
			<text class="tit">验证码</text>
			<input class="input" type="text" maxlength="10" v-model="info.verifyCode" placeholder="请输入验证码" placeholder-class="placeholder" />
		</view>
		<!-- <view class="cell row b-b">
			<text class="tit fill">设为默认地址</text>
			<switch :checked="info.isDefault" color="#FF536F" @change="onSwitchChange" />
		</view> -->
		
		<view class="bot-btn-wrap fix-bot bg-none">
			<mix-button ref="confirmBtn" text="下一步" icon="icon-jia2" :iconSize="36" :isConfirm="false" @onConfirm="submit"></mix-button>
		</view>
		<tui-modal></tui-modal>
		<loading></loading>
	</view>
</template>

<script>
	import mixButton from '@/components/mix-button/mix-button.vue';
	export default {
		components: {
			mixButton
		},
		data() {
			return {
				info: {
					agencyId:'',
					phoneNum:'',
					verifyCode:'',
				},
				countFun:'',
				verifyWord: '获取验证码', // 默认值
				disabledCodeBtn:false, //控制倒计时的时候点击不起作用
			}
		},
		onLoad(options) {
			// #ifdef H5
			   this.$wechat.noShare();
			// #endif
			if(options.data){
				// this.info = JSON.parse(options.data)
				// this.defaultValue[0]=this.info.province;
				// this.defaultValue[1]=this.info.city;
				// this.defaultValue[2]=this.info.district;
				// this.type='update';
			}
		},
		methods: {
			submit(){
				const data = this.info;
				if(!data.agencyId){
					this.$util.msg('请输入会员码');
					this.$refs.confirmBtn.stop();
					return;
				}
				if(!this.$util.checkStr(data.phoneNum, 'mobile')){
					this.$util.msg('请输入正确的手机号码');
					this.$refs.confirmBtn.stop();
					return;
				}
				if(!data.verifyCode){
					this.$util.msg('请输入验证码');
					this.$refs.confirmBtn.stop();
					return;
				}
				let params=this.info;
					this.$http.checkVerifyCode(params).then((res) => {
						if(res.status === 0) {
							if(res.data==='1'){
								uni.showToast({
									title: '请重新发送验证码',
									icon: "none"
								});
								clearTimeout(this.countFun)
								this.disabledCodeBtn = false;
								this.verifyWord = "获取验证码"
							}else if(res.data==='2'){
								uni.showToast({
									title: '验证码错误',
									icon: "none"
								});
							}else if(res.data==='3'){
								uni.showToast({
									title: '验证码已失效',
									icon: "none"
								});
							}else  if(res.data==='4'){
								uni.navigateTo({
									url: `../IDCardUpload/IDCardUpload?agencyId=${this.info.agencyId}&phoneNum=${this.info.phoneNum}`
								})
							}else {
								uni.navigateTo({
									url: `../newPassword/newPassword?agencyId=${this.info.agencyId}`
								})
							}
						}
					})
			},
			sendVerifycode(){
				const data = this.info;
				if(this.disabledCodeBtn){
					return;
				}
				if(!data.agencyId){
					this.$util.msg('请输入会员码');
					this.$refs.confirmBtn.stop();
					return;
				}
				if(!this.$util.checkStr(data.phoneNum, 'mobile')){
					this.$util.msg('请输入正确的手机号码');
					this.$refs.confirmBtn.stop();
					return;
				}
				this.countDown(60);
					//获取手机验证码
					const param={};
					param.agencyId=this.info.agencyId;
					param.phoneNum=this.info.phoneNum;
					this.$http.sendVerifyCode(param).then((res) => {
					  if (res.status === 0) {
						console.log(res.data);
						if(res.data=='1'){
							uni.showToast({
								title: '该会员码不存在',
								icon: "none"
							});
							// this.countDown (0)
							clearTimeout(this.countFun)
							this.disabledCodeBtn = false;
							this.verifyWord = "获取验证码"
						}else if(res.data=='2'){
							uni.showToast({
								title: '验证码发送失败',
								icon: "none"
							});
							clearTimeout(this.countFun)
							this.disabledCodeBtn = false;
							this.verifyWord = "获取验证码"
						}else if(res.data=='3'){
							uni.showToast({
								title: '该会员码已经提交过申请',
								icon: "none"
							});
							clearTimeout(this.countFun)
							this.disabledCodeBtn = false;
							this.verifyWord = "获取验证码"
						}
					  }
					}).catch(() => {
						clearTimeout(this.countFun)
						this.disabledCodeBtn = false;
						this.verifyWord = "获取验证码"
					});
				
			},
			// 倒计时方法
			countDown (time) {
						if (time === 0) {
						  this.disabledCodeBtn = false;
						  this.verifyWord = "获取验证码"
						  return;
						} else {
						  this.disabledCodeBtn = true;
						  this.verifyWord = '重新发送(' + time + ')'
						  time--;
						}
						this.countFun=setTimeout(()=> {
						  this.countDown(time)
						}, 1000)
			},
			onSwitchChange(e){
				this.data.is_default = e.detail.value;
			}
		}
	}
</script>

<style scoped lang="scss">
	.app{
		padding: 10rpx 36rpx 0;
		background-color: #fff;
		height: 100vh;
	}
	.cell{
		height: 106rpx;
		
		.tit{
			min-width: 140rpx;
			font-size: 28rpx;
			color: #111111;
		}
		.input{
			flex: 1;
			font-size: 30rpx;
			color: #333;
		}
		.icon-you{
			flex-shrink: 0;
			margin-right: 8rpx;
			margin-left: 40rpx;
			font-size: 24rpx;
			color: #aaa;
		}
		switch{
			transform: scale(0.8) translateX(10rpx);
			transform-origin: center right;
		}
	}
</style>
